package com.aijk.ehm.sys.api;

import com.aijk.base.entity.PagingRequest;
import com.aijk.commons.exception.ErrorCodeConst;
import com.aijk.commons.exception.ErrorCodeException;
import com.aijk.commons.mijo.utils.PojoUtil;
import com.aijk.commons.utils.CheckUtils;
import com.aijk.ehm.sys.service.BasRegionService;
import com.aijk.ehm.sys.model.BasRegion;
import com.aijk.ehm.sys.model.qo.BasRegionDto;
import com.aijk.ehm.sys.model.qo.BasRegionQo;
import com.aijk.ehm.sys.model.vo.BasRegionVo;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 接口实现。
 *
 */
@Service("basRegionApiImpl")
@Transactional
public class BasRegionApiImpl implements BasRegionApi {

    private final Logger logger = LoggerFactory.getLogger(BasRegionApiImpl.class);

    @Autowired
    private BasRegionService basRegionService;

    @Override
    public Integer saveBasRegion(BasRegionDto dto) throws ErrorCodeException {
        CheckUtils.check(ErrorCodeConst.BASIC_MODULE_DUBBO,dto.getTitle(), "title");
        CheckUtils.check(ErrorCodeConst.BASIC_MODULE_DUBBO,dto.getLevel(), "level");
        BasRegion basRegion = PojoUtil.convert(dto, BasRegion.class);
        basRegionService.save(basRegion);
        return basRegion.getId();
    }

    @Override
    public void updateBasRegion(BasRegionDto dto) throws ErrorCodeException {
        CheckUtils.check(ErrorCodeConst.BASIC_MODULE_DUBBO,dto.getId(), "id");
        CheckUtils.check(ErrorCodeConst.BASIC_MODULE_DUBBO,dto.getTitle(), "title");
        CheckUtils.check(ErrorCodeConst.BASIC_MODULE_DUBBO,dto.getLevel(), "level");
        BasRegion basRegion = PojoUtil.convert(dto, BasRegion.class);
        basRegionService.update(basRegion);
    }

    @Override
    public BasRegionVo findBasRegion(Integer id) throws ErrorCodeException {
        CheckUtils.check(ErrorCodeConst.BASIC_MODULE_DUBBO,id, "id");
        BasRegion basRegion = basRegionService.findOne(id);
        return PojoUtil.convert(basRegion, BasRegionVo.class);
    }

    @Override
    public List<BasRegionVo> listBasRegion(BasRegionQo qo) {
     //   SmsUtils.send("18158133355","您的密码为1234。请不要泄露给其他人。");
        List<BasRegion> basRegions = basRegionService.findAll(qo);
        return PojoUtil.convert(basRegions, BasRegionVo.class);
    }

    @Override
    public void dropBasRegion(Integer id) throws ErrorCodeException {
        CheckUtils.check(ErrorCodeConst.BASIC_MODULE_DUBBO,id, "id");
        basRegionService.delete(id);
    }

    @Override
    public int count(BasRegionQo qo) throws ErrorCodeException {
        return basRegionService.count(qo);
    }

    @Override
    public PageInfo<BasRegionVo> pageBasRegion(BasRegionQo qo) {
        BasRegionQo basRegionQo = PojoUtil.convert(qo, BasRegionQo.class);
        PagingRequest pagingRequest = new PagingRequest(qo.getPageNo().intValue(), qo.getPageSize().intValue());
        PageInfo<BasRegion> basRegionPageInfo = basRegionService.pageList(pagingRequest, basRegionQo);
        List<BasRegion> basRegions = basRegionPageInfo.getList();
        List<BasRegionVo> basRegionVos = PojoUtil.convert(basRegions, BasRegionVo.class);
        PageInfo<BasRegionVo> basRegionVoPageInfo = PojoUtil.convert(basRegionPageInfo, PageInfo.class);
        basRegionVoPageInfo.setList(basRegionVos);
        return basRegionVoPageInfo;
    }
}
